import {arrayToObject} from '../../../../assets/js/util.js'
export default{
  data() {
    return{
      loading: false,
    	// 默认参数 和 URL
      SRC: '',
      DP: {
        p: '',
        f: ''
      },
			title:'额度更新流水',
			// 设置时间 一周， 一月，  昨天
			pickerOptions: {
      	shortcuts: [
	      	{
	          text: '最近一周',
	          onClick(picker) {
	            const end = new Date();
	            const start = new Date();
	            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
	            picker.$emit('pick', [start, end]);
	          }
	        }, {
	          text: '最近一个月',
	          onClick(picker) {
	            const end = new Date();
	            const start = new Date();
	            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
	            picker.$emit('pick', [start, end]);
	          }
	        }, {
	          text: '最近三个月',
	          onClick(picker) {
	            const end = new Date();
	            const start = new Date();
	            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
	            picker.$emit('pick', [start, end]);
	          }
	        }
        ]
    	},
			// 存放获取到的区服数
			serviceAreas: [],
			// 提交的表单
		  form: {
		  	// 时间
				dateOne: '',
		  	dateStart: '',
		  	dateEnd: '',
		  	serviceArea: '0',
		  	auID: ''
		  },
		  // 表单验证
		  rules: {
        serviceArea: [
          { required: true, message: '请选择区服', trigger: 'change' }
        ],
        auID: [
          { required: true, message: '请输入账号ID', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        dataOne: [
          { required: true, message: '请选择日期', trigger: 'blur' },
        ]
      },
		  // 账号ID输入框的宽
			inputWidth:'220px',
			// 所有el-form-item label的宽度
		  formLabelWidth: '120px',
    	/*表格数据*/
    	tableData:[],
    	/*realTimeServiceContent 的宽*/
    	realTimeServiceContent:'100%',
    	/*表的宽度*/
    	realTimeServiceTableWidth: "100%",
    	/*数据表头*/
    	tableHeader:[
				{prop:'innerId',label:'时间'},
        {prop:'plat',label:'区服'},
        {prop:'channel',label:'设备mac地址'},
        {prop:'serviceArea',label:'账号ID'},
        {prop:'accountId',label:'减少额度'},
        {prop:'roleId',label:'增加额度'},
        {prop:'roleGrade1',label:'操作结果码'},
        {prop:'roleGrade2',label:'减少总额'},
        {prop:'VIPGrade',label:'增加总额'}
    	],
      currentPage: 1
    }
  },
	computed:{
		xlsx(){
			return this.title+'.xlsx'
		}
	},
  methods: {
  	// 通用 成功 与 失败
    successFun(data) {
      this.$message({
        message: data,
        type: 'success'
      });
    },
    errorFun(data) {
      this.$message.error(data);
    },
  	// 时间改变 得到起始时间和结束时间
  	dateChange(val){
  		var t = val.split(' - ')
  		this.form.dateStart = t[0];
  		this.form.dateEnd = t[1];
  	},
  	// 提交数据
  	commitData() {
  		var that = this;
  		var p = new Promise(function(resolve,reject){
  			 var obj = {
  			 		"dateStart": that.form.dateStart,
  			 		"dateEnd": that.form.dateEnd,
  			 		"userid": that.form.auID,
  			 		"serverid":that.form.serviceArea
  			 }
         that.loading = true;
         that.tableData = [];
         obj = encodeURI(JSON.stringify(obj));
  			 that.$http.jsonp(`${that.SRC}data/get.do?p=${that.DP.p}&f=${that.DP.f}&a=search`,{
          params:{'ps':'params='+obj},
        }).then(function(res){
  			 		resolve(res.body)
  			 	}
  			 )
  		})
  		p.then(function(value){
        that.loading = false;
  			var data = JSON.parse(JSON.parse(value));
  			if(parseInt(data["res"]) === 0){
          if(data.data.length === 0){
            that.errorFun("该账号暂无数据！")
          }else{
            that.tableData = arrayToObject(that.tableHeader,data.data);
          }
  			}else{
  				that.errorFun(data["msg"])
  			}
  		})
  	},
  	// 搜索
		search(form){
			this.$refs[form].validate((valid) => {
        if (valid) {
         	this.commitData()
        } else {
          this.errorFun('请完善输入的值！');
          return false;
        }
      });
		},
		//重置
    reset(form) {
      this.$refs[form].resetFields();
    },
  	/*获取区服列表数据*/
  	getData: function(){
  		var that = this
  		var p = new Promise(function(resolve,reject){
  			that.$http.jsonp(`${that.SRC}data/get.do?p=${that.DP.p}&f=${that.DP.f}&a=initParam`,{
          params:{'ps': '' }
        }).then(function(res){
  				resolve(res.body)
  			})
  		})
  		p.then(function(value){
  			var data = JSON.parse(JSON.parse(value));
  			if(parseInt(data["res"]) === 0){
  				for(var i in data["serverItmes"]){
  					that.serviceAreas.push({
  						label : i,
  						value : data["serverItmes"][i]
  					})
  				}
  			}else{
  				that.getErrorMsg(Number(data.res))
  			}
  		})
  	},
  	/*设置初始值*/
  	_init(){
  		window.addEventListener('resize', function() {
  			/*修改表格的宽度*/
        		this.realTimeServiceTableWidth = document.body.clientWidth-240+"px";
        		this.realTimeServiceContent = document.body.clientWidth-220+"px";
      	}.bind(this))
  	},
  	// 表格下载
  	excel() {
  		this.downloadExl(this.tableHeader,this.tableData,this.title,this.title);
  	}
  },
  mounted() {
   	this._init()
   	// 初始化URL
    this.SRC = this.$store.getters.src;
    this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
    this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
    this.form.dateOne = [new Date(new Date().getTime() - 86400000),new Date()];
   	this.getData()
   	// this.getData(this.pageSize,this.currentPage)
  }
}